﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;
using System.Web;
using YouHP.Model.Group;

namespace YouHP.Pages
{
    public class TreatmentAll : BasePage
    {
        protected List<string> mPingYin = new List<string>();
       
        protected override void ShowPage()
        {
            base.ShowPage();
            if(ErrorCount > 0) return;
            
            HandleCommon();
            if(ErrorCount > 0) return;
            
            if(IsPost) HandlePost();
            else HandleGet();
        }

        protected List<TopicInfo> TopicDataListHottest;
        protected List<TopicInfo> TopicDataListLatest;
        protected List<UserInfo> UserDataListLatest;

        // 和当前登录用户使用治疗最接近的病友
        protected List<UserInfo> bestFitPatientsByTreatments;

        // 治疗当前登录用户所患疾病的常用治疗
        protected string diseaseName = string.Empty;
        protected List<TreatmentAndUsedCount> mostFrequentUsedTreatmentsOfDisease;

        protected List<TreatmentInfo> TreatmentDataList;
        protected List<DiseaseInfo> DiseaseLst;
        protected int TreatmentCount = 0;
        protected int TreatmentPageCount = 0 ;
        protected int TreatmentPageSize = 15;
        protected int TreatmentPageIndex = 1;
        protected string TreatmentPagingString;

        protected string ItemUrl = "/Treatment.aspx?id=";
        protected string ItemOperName = "查看";

        protected void HandleCommon()
        {
            this.AddLocation("西药列表", "/TreatmentAll.aspx");
            DiseaseLst = DiseaseBLL.Instance.GetAllDiseaseByName("");
            this.SetTitle("西药列表");

            for (char c = 'A'; c <= 'Z'; c++)
            {
                mPingYin.Add(c.ToString());
            }
        }
        
        protected void HandlePost()
        {
            
        }

        protected void HandleGet()
        {
            int topiccnt = 8;
            TopicDataListHottest = TopicBLL.Instance.GetHottestTopic(topiccnt);
            TopicDataListLatest = TopicBLL.Instance.GetLastestTopic(topiccnt);
            UserDataListLatest = PatientBLL.Instance.GetLastestPatient(topiccnt);

            TreatmentPageIndex = RequestTools.GetQueryInt("page", 1);

            // 如果用户已经登录，获取和当前登录用户使用治疗最接近的病友列表
            if (UserData.Id > 0)
            {
                PatientRecordInfo prInfo = PatientBLL.Instance.GetPatientRecordInfo(UserData.Id);
                if (prInfo.DiseaseId > 0)
                {
                    UserData.DiseaseId = prInfo.DiseaseId;

                    bestFitPatientsByTreatments = PatientTreatmentBLL.Instance.GetBestFitTreatmentsPatientList(prInfo.Id);

                    if (bestFitPatientsByTreatments != null && bestFitPatientsByTreatments.Count > 4)
                        bestFitPatientsByTreatments = bestFitPatientsByTreatments.GetRange(0, 4);

                    // 获得治疗当前登录用户所患疾病的常用治疗
                    mostFrequentUsedTreatmentsOfDisease =
                        DiseaseBLL.Instance.GetMostFrequentUsedTreatmentsOfDisease(prInfo.DiseaseId, 4);

                    diseaseName = DiseaseBLL.Instance.GetDiseaseInfo(prInfo.DiseaseId).Name;
                }

            }

            TreatmentSearchCondition cond = new TreatmentSearchCondition();
            cond.DiseaseId = RequestTools.GetQueryInt("dszid", 0);
            cond.DiseaseName = RequestTools.GetQueryString("dszname");
            cond.TreatmentName = RequestTools.GetQueryString("trmname");

            TreatmentDataList = TreatmentBLL.Instance.GetTreatmentList(
                TreatmentPageSize, TreatmentPageIndex,
                cond, out TreatmentCount);

            TreatmentPageCount = Utils.GetPageCount(TreatmentCount, TreatmentPageSize);

            string query = HttpContext.Current.Request.Url.Query;
            query = Utils.RemoveQueryItem(query, "page");

            TreatmentPagingString = this.GetPagingString(
                TreatmentPageIndex,
                TreatmentPageCount,
                "/TreatmentAll.aspx" + query, "page");

            string sel = RequestTools.GetQueryString("sel");
            if (sel == "th")
            {
                this.ItemUrl = "/TreatmentHistory/HistoryStart.aspx?trmid=";
                this.ItemOperName = "选择";
            }

        }      
    }
}